package com.gitee.wsl.mathematics.geometry.d2.rect.ext

import com.gitee.wsl.mathematics.geometry.d2.rect.RectangleShape2d
import com.gitee.wsl.mathematics.number.compareTo

/**
 * If the viewport specified by left,top,right,bottom intersects this viewport, return true and set this viewport to
 * that intersection, otherwise return false and do not change this viewport. No check is performed to see if either
 * viewport is empty. Note: To just test for intersection, use intersects()
 *
 * @param left   The left side of the viewport being intersected with this viewport
 * @param top    The top of the viewport being intersected with this viewport
 * @param right  The right side of the viewport being intersected with this viewport.
 * @param bottom The bottom of the viewport being intersected with this viewport.
 * @return true if the specified viewport and this viewport intersect (and this viewport is then set to that
 * intersection) else return false and do not change this viewport.
 */
fun<N:Number> RectangleShape2d<N, *,*>.intersects(left: N, top: N, right: N, bottom: N): Boolean {
    if (this.left < right && left < this.right && this.bottom < top && bottom < this.top) {
       /* if (this.left < left) {
            this.left = left
        }
        if (this.top < top) {
            this.top = top
        }
        if (this.right > right) {
            this.right = right
        }
        if (this.bottom > bottom) {
            this.bottom = bottom
        }*/
        return true
    }
    return false
}

/**
 * If the specified viewport intersects this viewport, return true and set this viewport to that intersection,
 * otherwise return false and do not change this viewport. No check is performed to see if either viewport is empty.
 * To just test for intersection, use intersects()
 *
 * @param v The viewport being intersected with this viewport.
 * @return true if the specified viewport and this viewport intersect (and this viewport is then set to that
 * intersection) else return false and do not change this viewport.
 */
infix fun<N:Number> RectangleShape2d<N, *,*>.intersects(v: RectangleShape2d<N, *,*>) = intersects(v.left, v.top, v.right, v.bottom)

infix fun<N:Number> RectangleShape2d<N, *,*>.intersectsX(that:  RectangleShape2d<N, *,*>): Boolean
                                   = that.left <= this.right && that.right >= this.left

infix fun<N:Number> RectangleShape2d<N, *,*>.intersectsY(that:  RectangleShape2d<N, *,*>): Boolean
                                   = that.top <= this.bottom && that.bottom >= this.top


fun<N:Number> RectangleShape2d<N, *,*>.intersectionOrNull(that: RectangleShape2d<N, *,*>): RectangleShape2d<N, *,*>?
= if (this intersects that)
    create(
        max(this.left, that.left), max(this.top, that.top),
        min(this.right, that.right), min(this.bottom, that.bottom)
    ) as RectangleShape2d<N, *,*> else null

/*
fun<N:Number> RectangleShape2d<N, *>.intersection(that: RectangleShape2d<N, *>): RectangleShape2d<N, *> =
    if (this intersects that)
        create(
          max(this.left, that.left), max(this.top, that.top),
          min(this.right, that.right), min(this.bottom, that.bottom)
        ) else Rectangle.NIL*/
